package fk_snake

import (
	"sync"
)

type appService interface {
	Start() error
	Stop()
}

type App struct {
	services []appService
	wg sync.WaitGroup

	startedCount int
}

func (app *App) Start() error {
	for _, s := range app.services {
		if err := s.Start(); err != nil {
			s.Stop()
			return err
		}
		app.startedCount++
		app.wg.Add(1)
	}
	return nil
}

func (app *App) Stop() {
	for i := 0; i < app.startedCount; i++ {
		app.services[i].Stop()
		app.wg.Done()
	}
}

func (app *App) Wait() {
	app.wg.Wait()
}
